package iziswing.composant;

import java.awt.Container;
import java.awt.Point;

import javax.swing.JComponent;

import metier.ContexteComposant;

import org.apache.log4j.Logger;

import exception.ComposantException;

public class DefautComposant {

	private static Logger mLogger = Logger
			.getLogger(JTextField.class.getName());

	@SuppressWarnings("unchecked")
	public static Object newInstance(Container aFenetre, ContexteComposant aContexte,
			Object aComposantOut) throws ComposantException {
		if (aComposantOut == null) {
			try {
				Class<Object> lComposant = (Class<Object>) Class
						.forName(aContexte.getModeleClasse().getClasse());
				aComposantOut = lComposant.newInstance();
				if(aComposantOut instanceof JComponent){
					JComponent lJComposant = (JComponent)aComposantOut;
					Point lPoint = ComposantTools.getSize(aContexte);
					lJComposant.setBounds(aContexte.getX(), aContexte.getY(),
						lPoint.y, lPoint.x);
					aFenetre.add(lJComposant);
				}
			} catch (ClassNotFoundException e) {
				mLogger.debug(e);
			} catch (InstantiationException e) {
				mLogger.debug(e);
			} catch (IllegalAccessException e) {
				mLogger.debug(e);
			}
		}
		return aComposantOut;
	}
}
